<?php
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class Permissions extends Model
{
    protected $table = 'permissions';
    public $timestamps = true;

    public static function getList($where=[['menu_type', '=', '1']])
    {
        $rt = DB::table('permissions')->where($where)->get()->toArray();
        return $rt;
    }

    /**
     * 条件查询菜单
     * @param $where
     * @param $flag 1:字段有id;0：字段没有id
     * @return array
     */
    public static function getListByWhere($where,$flag=1)
    {
        if($flag){
            $rt = DB::table('permissions')->where('id', '<>',$where['id'] )
                ->where(function ($query) use($where) {
                    $query->where('title', '=', $where['title'])
                        ->orWhere('name', '=', $where['name'])
                        ->orWhere('url', '=', $where['url']);
                })->get()->toArray();
        }else{
            $rt = DB::table('permissions')
                ->where(function ($query) use($where) {
                    $query->where('title', '=', $where['title'])
                        ->orWhere('name', '=', $where['name'])
                        ->orWhere('url', '=', $where['url']);
                })->get()->toArray();
        }
        return $rt;
    }

    /**
     * 删除菜单
     * @param $where
     * @return false|int
     */
    public function delByWhereIn($where=[])
    {
        if($where){
            return DB::table('permissions')->whereIn('id', $where)->delete();
        }else{
            return false;
        }
    }
}
